<?php
/*
 * 售后
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\AfterSales;
use app\adminbackstage\model\Order;
use app\adminbackstage\model\Operation;

use app\common\model\UserAmount;
use app\common\model\Detailed;
use think\facade\Db;

class AfterSalesController extends BaseController{

    //售后列表
    public function afterSalesList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $phone = Request::param('phone');
        $username = Request::param('username');
        $o_pro_num = Request::param("o_pro_num");
        $p_num = Request::param('p_num');
        $as_state = Request::param('state');

        $where = [];
        $where[] = ['a.enable','=','1'];
        if(!empty($p_name)){
//            $where[] = ['o.p_name','=',$p_name];
            $where[] = ['o.p_name','like',"%$p_name%"];
        }
        
        if(!empty($phone)){
            $where[] = ['u.phone','=',$phone];
        }
        
        if(!empty($as_state)){
            $where[] = ['a.as_state','=',$as_state];
        }
        if(!empty($p_num)){
            $where[] = ['o.p_num ','=',$p_num ];
        }
        if(!empty($username)){
            $where[] = ['u.username','=',$username];
        }
        if(!empty($o_pro_num)){
            $where[] = ['o.o_pro_num','=',$o_pro_num];
        }
//        if(!empty($type)){
//            $where[] = ['type','=',$type];
//        }
        $list = AfterSales::afterSalesList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = AfterSales::afterSalesCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //售后添加
    public function afterSalesAdd(){
        //$store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
//        $p_name = Request::param('p_name');
//        $phone = Request::param('phone');
//        $username = Request::param('username');
//        $o_pro_num = Request::param("o_pro_num");
        $oid =  Request::param("oid");
        $as_reason = Request::param("as_reason");//售后原因
        $as_rq = Request::param("as_rq");//售后数量
        $as_refund = Request::param("as_refund");//退款金额
        $remarks = Request::param("remarks");//备注
        $where = [];
        $where[] = ['id','=',$oid];
        $orderInfo = Order::orderInfo($where);
        if(empty($orderInfo)){
            return json(['code' => 0, 'msg' => lang('订单不存在'), 'data' => '', 'count' => '']);
        }
        
        if($orderInfo['order_quantity']<$as_rq){
            return json(['code' => 0, 'msg' => lang('订单数量不符'), 'data' => '', 'count' => '']);
        }
                
        if($orderInfo['low_price']<$as_refund){
            return json(['code' => 0, 'msg' => lang('订单金额不符'), 'data' => '', 'count' => '']);
        }
        
        if(empty($remarks)){
            return json(['code' => 0, 'msg' => lang('请填写原因'), 'data' => '', 'count' => '']);
        }
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        
        $where_as = [];
        $where_as[] = ['a.oid','=',$oid];
        $where_as[] = ['a.uid','=',$orderInfo['uid']];
        $where_as[] = ['a.as_state','=',1];
        $info_as = AfterSales::afterSalesInfo($where_as);
        if(!empty($info_as)){
            return json(['code' => 0, 'msg' => lang('售后已存在,请先处理'), 'data' => '', 'count' => '']);
        }
        
        $time = time();
        $data = [
            'as_reason' => $as_reason,
            'as_rq' => $as_rq,
            'as_refund' => $as_refund,
            'remarks' => $remarks,
            'p_id' => $orderInfo['p_id'],
            'uid' => $orderInfo['uid'],
            'oid' => $oid,
            'p_name' => $orderInfo['p_name'],
            'enable' => $enable,
            'addtime' => $time,
        ];

        AfterSales::afterSalesAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //售后详情
    public function afterSalesInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = AfterSales::afterSalesInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //售后修改
    public function afterSalesEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');

        $as_reason = Request::param("as_reason");//售后原因
        $as_rq = Request::param("as_rq");//售后数量
        $as_refund = Request::param("as_refund");//退款金额
        $remarks = Request::param("remarks");//备注
        $re_remarks = Request::param("re_remarks");//备注
        $enable = Request::param("enable");
        $as_state = Request::param("as_state");
        $update = [];
        if(!empty($as_reason)){
            $update['as_reason'] = $as_reason;
        }
        
        if(!empty($as_rq)){
            $update['as_rq'] = $as_rq;
        }
        
        if(!empty($as_refund)){
            $update['as_refund'] = $as_refund;
        }
        
        if(!empty($remarks)){
            $update['remarks'] = $remarks;
        }
        if(!empty($re_remarks)){
            $update['re_remarks'] = $re_remarks;
        }
        if(!empty($as_state)){
            $update['as_state'] = $as_state;
        }
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }
        
        $where_as = [];
        $where_as[] = ['a.id','=',$id];
        $info = AfterSales::afterSalesInfo($where_as);
        

        $where = [];
        $where[] = ['id','=',$info['oid']];
        $orderInfo = Order::orderInfo($where);
        if(empty($orderInfo)){
            return json(['code' => 0, 'msg' => lang('订单不存在'), 'data' => '', 'count' => '']);
        }
        if($orderInfo['order_quantity']<$as_rq){
            return json(['code' => 0, 'msg' => lang('订单数量不符'), 'data' => '', 'count' => '']);
        }
                
        if($orderInfo['low_price']<$as_refund){
            return json(['code' => 0, 'msg' => lang('订单金额不符'), 'data' => '', 'count' => '']);
        }
        if(empty($re_remarks) && $as_state != 3){
            return json(['code' => 0, 'msg' => lang('请填写原因'), 'data' => '', 'count' => '']);
        }
        if($orderInfo['state']<1){
            return json(['code' => 0, 'msg' => lang('订单状态错误'), 'data' => '', 'count' => '']);
        }
        
        Db::startTrans();//开启事务
        if($as_state==3){

            
            //资金流水记录
            $UserAmount = UserAmount::userAmountInfo(['uid'=>$info['uid']]);//获取用户可用余额
            $detailed = \detailed($info['uid'],$info['as_refund'],'after_sales_admin',$UserAmount['balance'],$id,'','');
            Detailed::detailedAdd($detailed);
            //用户资金扣除  转冻结
//            UserAmount::userAmountInc(["uid"=>$info['uid'],"id"=>$UserAmount["id"]],"freezing_amount",floatval($info['as_refund']));
            UserAmount::userAmountInc(["uid"=>$info['uid'],"id"=>$UserAmount["id"]],"balance",floatval($info['as_refund']));
        }
        AfterSales::afterSalesEdit($id,$update);
        
        
        //审核操作记录
        $Operation = new Operation();
        $data =[
            'uid' => $a_uid,
            'operation' => '自助充值审核',
            'main_table' => 'nh_after_sales',
            'mt_id' => $id,
            'addtime' => time(),
        ];
        $Operation->operationAdd($data);
        
        //全部退货改变订单状态
        $coun = 0;
        $where_as_t = [];
        $where_as_t[] = ['a.oid','=',$info['oid']];
        $where_as_t[] = ['a.uid','=',$orderInfo['uid']];
        $where_as_t[] = ['a.as_state','=',3];
        $afterSalesRq = AfterSales::afterSalesRq($where_as_t);
        if(!empty($afterSalesRq)){
            $coun = $afterSalesRq;
        }
        
        
        if($orderInfo['order_quantity']<= $coun){
            $update_o['state'] = -2;
            Order::orderEdit($info['oid'],$update_o);
        }

        Db::commit();//事务提交
        
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
